/*
 * output.c
 *
 *  Created on: 2012/09/24
 *      Author: owner
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "core.h"
#include "rule_info.h"

static CHAR FILEPATH[OS_FULLPATH];

static VOID xmlPathFull2Part(PSZ filepath, PSZ ws) {
	INT dir = 0;
	if (filepath == NULL || ws == NULL ) {
		return;
	}
	if (strlen(ws) < 1) {
		return;
	}
	dir = strlen(ws) - 1;
	while (ws[dir] == '/' || ws[dir] == '\\') {
		dir--;
		if (dir < 0) {
			return;
		}
	}
	strcpy(filepath, filepath + dir + 1);
	dir = 0;
	while (filepath[dir] == '/' || filepath[dir] == '\\') {
		dir++;
	}
	strcpy(filepath, filepath + dir);
}

static PSZ filePath(AST index, PSZ ws) {
	if (index > 0) {
		CFileIndex.getFilePath(index, FILEPATH, OS_FULLPATH);
		xmlPathFull2Part(FILEPATH, ws);
		return FILEPATH;
	} else {
		return "";
	}
}

INT writeXML_ENG(PSZ path, PSZ ws) {
	PRULEMAN curRule = stRuleManager;
	PRULEOUT node = NULL;
	FILE * file;

	file = fopen(path, "w");
	if (file == NULL ) {
		printf("can not open file %s\n", path);
		return EXIT_ERROR;
	}
	fprintf(file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>");

	while (curRule != NULL ) {
		node = *(curRule->headOut);
		while (node != NULL ) {
			if(node->beginLine > 0)
			{
			fprintf(file,
					"<message src=\"%s\" line=\"%d\" colstart=\"%d\" colend=\"%d\">"
							"<rule>%s</rule><rank>%d</rank><text>%s</text></message>",
					filePath(node->fileIndex, ws), node->beginLine,
					node->beginCol, node->endCol, node->ruleID, node->errRank,
					node->msgBuf_eng);
			}
			node = node->next;
		}
		curRule = curRule->nextRule;
	}
	fprintf(file, "</root>");
	fclose(file);
	return EXIT_SUCCESS;
}

INT writeXML_CHS(PSZ path, PSZ ws) {
	PRULEMAN curRule = stRuleManager;
	PRULEOUT node = NULL;
	FILE * file;

	file = fopen(path, "w");
	if (file == NULL ) {
		printf("can not open file %s\n", path);
		return EXIT_ERROR;
	}
	fprintf(file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>");

	while (curRule != NULL ) {
		node = *(curRule->headOut);
		while (node != NULL ) {
			fprintf(file,
					"<message src=\"%s\" line=\"%d\" colstart=\"%d\" colend=\"%d\">"
							"<rule>%s</rule><rank>%d</rank><text>%s</text></message>",
					filePath(node->fileIndex, ws), node->beginLine,
					node->beginCol, node->endCol, node->ruleID, node->errRank,
					node->msgBuf_chs);
			node = node->next;
		}
		curRule = curRule->nextRule;
	}
	fprintf(file, "</root>");
	fclose(file);
	return EXIT_SUCCESS;
}

INT writeXML_JPN(PSZ path, PSZ ws) {
	PRULEMAN curRule = stRuleManager;
	PRULEOUT node = NULL;
	FILE * file;

	file = fopen(path, "w");
	if (file == NULL ) {
		printf("can not open file %s\n", path);
		return EXIT_ERROR;
	}
	fprintf(file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>");

	while (curRule != NULL ) {
		node = *(curRule->headOut);
		while (node != NULL ) {
			fprintf(file,
					"<message src=\"%s\" line=\"%d\" colstart=\"%d\" colend=\"%d\">"
							"<rule>%s</rule><rank>%d</rank><text>%s</text></message>",
					filePath(node->fileIndex, ws), node->beginLine,
					node->beginCol, node->endCol, node->ruleID, node->errRank,
					node->msgBuf_jpn);
			node = node->next;
		}
		curRule = curRule->nextRule;
	}
	fprintf(file, "</root>");
	fclose(file);
	return EXIT_SUCCESS;
}
